Write the code that does the modifications

This report is currently based on all regions. We'll modify the selection criteria to include only data from the USA. But first you may need to add a reference to
your project.

  1. Close the Crystal Report Viewer.
  2. From the Project menu select References.
  3. Check the Microsoft ActiveX Data Objects Recordset 2.0 Library and click OK.
  4. Double-click Form2 from your Project Explorer to open it. This form contains the Report Viewer. Double-click on it to see the code in the main window. It should look something like this:

This code should appear in the General Declarations section.

Dim Report As New CrystalReport1

This code should appear in the Form_Load event procedure. It tells the viewer what report it should display and then it tells it to display (view) the report.

Private Sub Form_Load()

            Screen.MousePointer = vbHourGlass
            CRViewer1.ReportSource = Report
            CRViewer1.ViewReport
            Screen.MousePointer = vbDefault

End Sub

This code should appear in the Form_Resize event procedure, it resizes the viewer every time the form is resized.

Private Sub Form_Resize()

            CRViewer1.Top = 0
            CRViewer1.Left = 0
            CRViewer1.Height = ScaleHeight
            CRViewer1.Width = ScaleWidth

End Sub

  1. To add selection criteria to the report. Edit the "Form2" declarations and "Form_Load" procedure to look like this:


Dim Report As New CrystalReport1

Dim rs As New ADOR.Recordset

Private Sub Form_Load()

     Screen.MousePointer = vbHourGlass

     rs.Open "Select * from customer where country = 'USA'", _"xtreme sample database"

     Report.Database.SetDataSource rs,3,1

     CRViewer1.ReportSource = Report

     CRViewer1.ViewReport

     Screen.MousePointer = vbDefault

End Sub


This adds a query to select only the records with "USA" in the "Country" field. The recordset that results from that query will be passed to the report engine and the results displayed.

  1. Run the report again to see the changes. Your report should only contain USA data.
  2. Now we'll add a text object to the report and manipulate it using code. In the main Report Designer window, right-click just to the right of the graph in the Report Header pane and point to Insert, then select Text Object.
  3. Move the text object just above the pie graph. You may have to move the graph down a bit first. Make note of the Name in the Properties window. In this example, the name is "Text5" but the name in your project may be different.
  4. Now we'll set the contents of the text object you just added. Open the "Section5" code for editing in the Text Object by double-clicking on the Report Header [Section5] and add some code to the "Section5" "Format" procedure as shown.


Private Sub Section5_Format(ByVal pFormattingInfo As Object)

    Text5.SetText "Here is some text from my app"

    Text5.Font.Italic = True

    Text5.Font.Bold = True

    Text5.Font.Size = 14

End Sub


  1. This will set the text object, change the font to Italic, Bold 14 point and display the results. Run the report again to see the changes.

To complete this exercise, you'll now add some event-driven code to explore how you can make the run time viewer control more interactive.

  1. With the Form 2 code window open, use the Object and Procedure list boxes to select the "CRViewer1" object and add the "DrillOnGroup" and "PrintButtonClicked" procedures to the "Form2" code. Edit these new procedures as shown below:


Private Sub CRViewer1_DrillOnGroup(GroupNameList As
Variant, ByVal DrillType As
CRVIEWERLibCtl.CRDrillType, UseDefault As Boolean)

    MsgBox "You're drilling down on the " &
    GroupNameList(0) & " group!"

End Sub

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)

            MsgBox "You clicked the Print button!"

End Sub


  1. The "DrillOnGroup" procedure is triggered when the user double-clicks on any graph pie slice or report summary field. When this occurs, the text shown will appear in a message box. The "PrintButtonClicked" procedure works similarly when the viewer Print button is clicked. Try both of these procedures to make certain your code works as expected.

Now that you've seen a few simple ways to modify a report, you may want to explore some of the other properties and events in the report engine that you can use in your application. For more sophisticated programming examples, consult the sample applications. For more details on specific methods, properties, or events, use the VB Object Browser or consult the Crystal Reports Developer's Help (CrystalDevHelp.chm) or the Report Designer Component Help (Crrdc.hlp).



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com